package com.phrp.service.impl;

import com.phrp.exception.CustomizationException;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.phrp.entity.Dictionary;
import com.phrp.dao.DictionaryDao;
import com.phrp.service.DictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.phrp.util.returnResult.ResponseResult;
import com.phrp.util.returnResult.ReturnInfoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ssc
 * @since 2024-04-27
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryDao, Dictionary> implements DictionaryService {

    @Autowired
    private DictionaryDao dictionaryDao;

    /**
     * 获取用户状态列表
     *
     * @param tableName
     * @param fieldName
     * @return
     */
    @Override
    public ResponseResult getUserType(String tableName, String fieldName) {
        LinkedList<Dictionary> dictionarys = dictionaryDao.selectByName(tableName, fieldName);
        return new ResponseResult(ReturnInfoEnum.SUCCESS, dictionarys);
    }

    /**
     * 修改用户状态名称
     *
     * @param id
     * @param typeName
     * @return
     */
    @Override
    public ResponseResult changeTypeName(int id, String typeName) {
        UpdateWrapper<Dictionary> update = new UpdateWrapper<>();
        update.eq("d_id", id)
                .set("meaning", typeName);

        int i = dictionaryDao.update(null, update);
        if (i != 1) {
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    /**
     * 修改字典表的状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public ResponseResult updateStatus(int id, String status) {
        UpdateWrapper<Dictionary> update = new UpdateWrapper<>();
        update.eq("d_id", id)
                .set("status", status);

        int i = dictionaryDao.update(null, update);

        if (i != 1) {
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    /**
     * 删除字典表的数据
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = CustomizationException.class)
    @Override
    public ResponseResult deleteData(int id) {
        int i = dictionaryDao.deleteById(id);
        if (i != 1) {
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    /**
     * 添加用户类型
     * @param typeName
     * @return
     */
    @Override
    public ResponseResult addUserType(String typeName) {
        Dictionary dictionary = new Dictionary();
        dictionary.setTableName("user");
        dictionary.setFieldName("type");
        dictionary.setMeaning(typeName);
        dictionary.setCreateTime(LocalDateTime.now());
        dictionary.setUpdateTime(LocalDateTime.now());

        // 设置code，需要从数据库中获取所有的code，将其做一个比较，找出没有的那个数字进行插入
        String[] codes = dictionaryDao.getCodes("user", "type");

        // 调用方法获取不存在的最小值
        dictionary.setCodeNumber(findMissingNumber(codes));

        int insert = dictionaryDao.insert(dictionary);
        if (insert != 1) {
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    public String findMissingNumber(String[] arr) {
        Set<Integer> set = new HashSet<>();
        for (String str : arr) {
            set.add(Integer.parseInt(str));
        }

        int missing = 1;
        while (set.contains(missing)) {
            missing++;
        }

        return String.valueOf(missing);
    }
}
